package com.yaoli.niuke;

import com.yaoli.ajob.ListNode;

/**
 * [编程题]反转链表
 * Created by siemens on 5/9/2017.
 */
public class _008_Test {
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(3);
        ListNode node66 = new ListNode(4);
        ListNode node3 = new ListNode(7);

        node1.next = node2;
        node2.next = node66;
        node66.next = node3;

        _008_Test test = new _008_Test();
        test.ReverseList(node1);
    }
    public ListNode ReverseList(ListNode head) {
        // 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7

        //head <- 1     <- 2 <- 3 <- 4 <- 5 <- 6 <- 7
        //head <- 1     <- 2 <- 3 <- 4 <- 5 <- 6 <- 7
        //head <- 1 <- 2 <- 3 <- 4 <- 5 <- 6 <- 7
        //head <- 1 <- 2 <- 3 <- 4 <- 5 <- 6 <- 7
        //head <- 1 <- 2 <- 3 <- 4 <- 5 <- 6 <- 7


        ListNode point = null;


        while(head != null){
            ListNode temp = head;   //临时保存head
            head = head.next;       //将head.next 赋值给 head
            temp.next = point;      //将temp的下一个 指向 point
            point = temp;           //再将temp 指向当前的，又成为最后的节点
        }

        return point;
    }
}
